package com.example.server.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.Duration;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

public class Util {

    private static final String SECRET_KEY = "your_secret_key";

    private static final Duration EXPIRATION = Duration.ofSeconds(600);

    // JWT
    public static String generate(String id) {
        Date expireDate = new Date(System.currentTimeMillis() + EXPIRATION.toMillis());
        return Jwts.builder()
                .setSubject(id)
                .setIssuedAt(new Date())
                .setExpiration(expireDate)
                .signWith(SignatureAlgorithm.HS512, SECRET_KEY)
                .compact();
    }
    public static Claims parse(String token) {
        if (Strings.isEmpty(token)) {
            return null;
        }
        Claims claims;
        try {
            claims = Jwts.parser()
                    .setSigningKey(SECRET_KEY)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (ExpiredJwtException expiredJwtException) {
            System.err.println("token expired.");
            claims = null;
        }
        return claims;
    }

    public static String getSubject(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(token)
                .getBody();
        return claims.getSubject();
    }

public static long getRemainedTime(String token){
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(token)
                .getBody();
        return (int)(claims.getExpiration().getTime() - new Date().getTime()) / 1000;
}


    /**
     * 保存图片的方法
     * @param multipartFile
     * @param directory
     * @return
     */
    public static String savePicture(MultipartFile multipartFile,String directory){
        File path = new File(directory);
        //获取图片文件的后缀
        String extension = StringUtils.getFilenameExtension(multipartFile.getOriginalFilename());
        String filename = UUID.randomUUID() + "." + extension;

        try {
            multipartFile.transferTo(new File(path,filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filename;
    }

    public static String savePictures(MultipartFile[] multipartFiles,String directory){
        String[] filenames = new String[multipartFiles.length];
        for(int i = 0; i < multipartFiles.length; i++){
            filenames[i] = "\"" + savePicture(multipartFiles[i],directory) + "\"";
        }
        return Arrays.toString(filenames);
    }
}
